package kr.blogspot.stiner01.moduletutorial;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class ExternalStorageTest extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        TextView textView = new TextView(this);
        setContentView(textView);
        
        String state = Environment.getExternalStorageState();
        if (!state.equals(Environment.MEDIA_MOUNTED))
        {
            textView.setText("No external storage mounted");
        }
        else
        {
            File externalDir = Environment.getExternalStorageDirectory();
            File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt");
            
            try
            {
                writeTextFile(textFile, "This is a test. Roger");
                String text = readTextFile(textFile);
                textView.setText(text);
                
                if (!textFile.delete())
                {
                    textView.setText("Couldn't remove temporary file");
                }
            }
            catch (IOException e)
            {
                textView.setText("something went wrong! " + e.getMessage());
            }
        }
    }
    
    private void writeTextFile(File file, String text) throws IOException
    {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(text);
        writer.close();
    }
    
    private String readTextFile(File file) throws IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        StringBuilder text = new StringBuilder();
        String line;
        
        while ((line = reader.readLine()) != null)
        {
            text.append(line);
            text.append("\n");
        }
        reader.close();
        
        return text.toString();
    }
}

/*
public class ExternalStorageTest extends Activity
{
    StringBuilder _builder = new StringBuilder();
    TextView _textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        _textView = new TextView(this);
        _textView.setText("Test ExternalStorage.\n");
        setContentView(_textView);
        
        String state = Environment.getExternalStorageState();
        
        if (Environment.MEDIA_MOUNTED.equals(state))
        {
            File mountedPath = Environment.getExternalStorageDirectory();

            try
            {
                String data = "asdfasdfasdfasdfasdfasdf하모나허모베아로모";
                
                File storePath = new File(mountedPath.getAbsolutePath() + "/Android/data/" + getPackageName());
                storePath.mkdir();
                
                File storeFile = new File(storePath.getAbsolutePath() + "/data.txt");
                
                FileOutputStream outStream = new FileOutputStream(storeFile);
                outStream.write(data.getBytes("UTF8"));
                outStream.close();
                
                FileInputStream inStream = new FileInputStream(storeFile);
                byte[] buffer = new byte[inStream.available()];
                inStream.read(buffer);
                inStream.close();
                
                _builder.append(new String(buffer));
            }
            catch (IOException e)
            {
                _builder.append("Exception\n");
                _builder.append(e.toString());
            }
        }
        else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
        {
            _builder.append("Read only.\n");
        }
        else if (Environment.MEDIA_SHARED.equals(state))
        {
            _builder.append("Shared.\n");
        }
        
        _textView.setText(_builder.toString());
    }
}
*/